import { defineStore } from 'pinia'
import { ref } from 'vue'
import { userGetInfoService } from '../../api/user'

export const useUserStore = defineStore('user', () => {
    const token = ref('')
    //获取token后设置新token
    const setToken = (newToken) => {
        token.value = newToken
    }
    //退出登录 清除token
    const removeToken = () => token.value = ''
    //user信息
    const user = ref({})
    const getUser = async () => {
        //请求接口
        const res = await userGetInfoService()
        user.value = res.data
    }
    //设置用户信息
    const setUser = (obj) => {
        user.value = obj
    }
    return {
        token,
        setToken,
        removeToken,
        user,
        getUser,
        setUser
    }
}, {
    persist: true
})